//
//  CJOrient.swift
//  CJPack
//
//  Created by 陈警卫 on 2024-07-11.
//

import Foundation

/// 设备方向
public enum CJDeviceOrient: Int {
    case unknown   // 未知
    case port      // 竖直
    case portDown  // 倒立竖直
    case landLeft  // 横向左边
    case landRight // 横向右边
    case faceUp    // 屏幕向上
    case faceDown  // 屏幕向上
}

public class CJOrientDevice {
    
    private var didDeviceOrientationChange: ((CJDeviceOrient) -> ())? = nil
    
    public func deviceOrientation(_ didDeviceOrientationChange: @escaping (CJDeviceOrient) -> ()) -> Self {
        
        self.didDeviceOrientationChange = didDeviceOrientationChange
        
        UIDevice.current.beginGeneratingDeviceOrientationNotifications()
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(deviceOrientationChange),
                                               name: UIDevice.orientationDidChangeNotification,
                                               object: nil)
        return self
    }
    
    
    @objc private func deviceOrientationChange() {
        let orient = UIDevice.current.orientation
        var orient_common: CJDeviceOrient = .unknown
        switch orient {
        case .portrait:
            orient_common = .port
        case .portraitUpsideDown:
            orient_common = .portDown
        case .landscapeLeft:
            orient_common = .landLeft
        case .landscapeRight:
            orient_common = .landRight
        case .faceUp:
            orient_common = .faceUp
        case .faceDown:
            orient_common = .faceDown
        case .unknown:
            orient_common = .unknown
        @unknown default:
            orient_common = .unknown
        }
        if let didDeviceOrientationChange = didDeviceOrientationChange {
            didDeviceOrientationChange(orient_common)
        }
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self,
                                                  name: UIDevice.orientationDidChangeNotification,
                                                  object: nil)
        UIDevice.current.endGeneratingDeviceOrientationNotifications()
    }
}

/// 屏幕方向
public enum CJScreenOrient: Int {
    case port  // 竖向
    case land  // 横向
    case left  // 横向左边
    case right // 横向右边
    case auto  // 自动
}

public protocol CJOrientAppDelegate {
    var deviceOrientation: CJScreenOrient { get set }
}

public struct CJOrientScreen {
    
    // MARK: 设置屏幕旋转
    /// 设置屏幕旋转
    func switchScreenOrientation(vc: UIViewController, orientation: CJScreenOrient) {
        var appDelegate =  UIApplication.shared.delegate as? CJOrientAppDelegate
        appDelegate?.deviceOrientation = orientation
        if #available(iOS 16.0, *) {
            let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
            switch orientation {
            case .port:
                windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait))
                break
            case .land:
                windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscape))
                break
            case .left:
                windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeLeft))
                break
            case .right:
                windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight))
                break
            case .auto:
                windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .all))
                break
            }
            vc.setNeedsUpdateOfSupportedInterfaceOrientations()
        } else {
            switch orientation {
            case .port:
                /// 强制设置成竖屏
                UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
                break
            case .land:
                /// 强制设置成横屏
                UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
                break
            case .left:
                /// 强制设置成横屏
                UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
                break
            case .right:
                /// 强制设置成横屏
                UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
                break
            case .auto:
                /// 设置自动旋转
                UIDevice.current.setValue(UIInterfaceOrientation.unknown.rawValue, forKey: "orientation")
                break
            }
        }
    }
}
